﻿using System;
using System.Linq;
using System.IO;
using OpenExcel.OleDb;

namespace OpenExcel_Examples
{
    public static class Example3
    {
        public static void Run()
        {
            Console.WriteLine("=== Running Example3 ===");
            string inputFile = Path.Combine(Program.OutputDirectory, "example3.xls");
            if (!File.Exists(inputFile))
            {
                Console.WriteLine("To run this example:\n" +
                                  "Open 'example2.xlsx' in Excel and save as 'example3.xls'\n" +
                                  "(as Excel 97-2003 Workbook)");
                return;
            }
            using (OleDbExcelReader rdr = new OleDbExcelReader(inputFile))
            {
                foreach (var w in rdr.Worksheets)
                {
                    Console.WriteLine("Contains worksheet: " + w.Name);
                }

                OleDbExcelWorksheet wsheet = rdr.Worksheets["My Order"];

                // Find row with "TOTAL"
                uint row = 0;
                do
                {
                    row++;
                }
                while (wsheet.Cells[row, 1].Value as string != "TOTAL");

                Console.WriteLine("Read TOTAL: " + wsheet.Cells[row, 5].Value);
            }
        }
    }
}
